package com.rent.utils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.rent.bean.Brand;
import com.rent.bean.Car;
import com.rent.bean.Rent;
import com.rent.bean.Type;
import com.rent.bean.User;

public class DataAccessUtils{
	/**
	 * 解析brand类
	 * @param r
	 * @return
	 */
	public List<Brand> parseBrand(ResultSet r) {
		List<Brand> list = new ArrayList<Brand>();
		try {
			while(r.next()) {
				Brand brand = new Brand(r.getInt("bid"),r.getString("brand"));
				list.add(brand);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 解析type类
	 * @param r
	 * @return
	 */
	public List<Type> parseType(ResultSet r) {
		List<Type> list = new ArrayList<Type>();
		try {
			while(r.next()) {
				Type brand = new Type(r.getInt("tid"),r.getInt("bid"),r.getString("type"),r.getDouble("price"),r.getString("brand"));
				list.add(brand);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Rent> parseRent(ResultSet rs){
		List<Rent> list = new ArrayList<Rent>();
		try {
			while(rs.next()) {
				Rent rent = new Rent(
						rs.getInt("rid"),
						rs.getString("uname"),
						rs.getInt("cid"),
						rs.getString("rtime"),
						rs.getString("btime"),
						rs.getDouble("money"),
						rs.getInt("status"),
						rs.getDouble("car_deposit"),
						rs.getDouble("violation_deposit"),
						rs.getInt("car_deposit_status"),
						rs.getInt("violation_deposit_status"),
						rs.getString("car_deposit_describe"),
						rs.getString("violation_deposit_describe")
					);
					Car car = new Car(
						rs.getInt("carid"),
						rs.getString("brand"),
						rs.getDouble("price"),
						rs.getInt("cstatus"),
						rs.getInt("cgrade"),
						rs.getString("type"),
						rs.getInt("isdelete"),
						rs.getInt("seat"),
						rs.getString("gearbox"),
						rs.getString("carNumber"),
						rs.getString("engineNumber"),
						rs.getInt("gasolineVolume"),
						rs.getInt("gasolineActualVolume"),
						rs.getInt("gasolineNumber"),
						rs.getString("color")
					);
					rent.c = car;
					list.add(rent);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Car> parseCar(ResultSet rs){
		List<Car> list = new ArrayList<Car>();
		try {
			while(rs.next()) {
				Car car = new Car(
						rs.getInt("carid"),
						rs.getString("brand"),
						rs.getDouble("price"),
						rs.getInt("cstatus"),
						rs.getInt("cgrade"),
						rs.getString("type"),
						rs.getInt("isdelete"),
						rs.getInt("seat"),
						rs.getString("gearbox"),
						rs.getString("carNumber"),
						rs.getString("engineNumber"),
						rs.getInt("gasolineVolume"),
						rs.getInt("gasolineActualVolume"),
						rs.getInt("gasolineNumber"),
						rs.getString("color")
					);
					list.add(car);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	public List<User> parseUserByVip(ResultSet rs) {
		List<User> list = new ArrayList<>();
		try {
			while(rs.next()) {
				User u = new User(rs.getInt("uid"),rs.getString("uname"),rs.getString("upassword"),rs.getInt("power"),
						rs.getString("idname"),rs.getString("idcard"),rs.getString("phonenumber"),
						rs.getDouble("balance"),rs.getInt("confirminfo"));
				u.vip.setOpentime(rs.getString("opentime"));
				u.vip.setLimittime(rs.getString("limittime"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}
